package controller

import (
	"errors"
	"math/rand"

	"gddgame.cc/galaxy/common/api/aliyun_api"
	. "gddgame.cc/galaxy/common/dsl/web"
	"gddgame.cc/galaxy/core/models"
	"gddgame.cc/galaxy/utils/def"
)

var (
	// todo 通过config服务加载配置信息
	authInfo = aliyun_api.NewAuthInfo("203851953", "EBQDdKTE2AN6ZWF5aKibygPRObuRwIWO", "ce4331b690c34903a42d97805b313597")
)

func SetRealInfo(context def.Context, app *models.App, openId string, idCard string, realName string) error {
	userOpen, err := GetOpen(context, app, openId)
	if err != nil {
		return err
	}

	if rand.Int31n(2) > 0 {
		if result, err := client.VerifyIdCard(*authInfo, idCard, realName); err != nil {
			return err
		} else {
			if !result.Result.IsOk {
				return EmptyExceptionDetail("用户不存在", errors.New("id_card is error"))
			}
		}
	} else {
		if result, err := client.VerifyIdCard2(*authInfo, idCard, realName); err != nil {
			return err
		} else {
			if !result.Result.IsOk {
				return EmptyExceptionDetail("用户不存在", errors.New("id_card is error"))
			}
		}
	}

	return UpdateUser(context, userOpen.UserId, map[string]interface{}{
		"id_card":   idCard,
		"real_name": realName,
	})
}
